<?php use_helper('Object', 'Validation', 'ObjectAdmin', 'I18N', 'Date') ?>
<title><?= $sf_response->getTitle() ?></title>
<?php use_stylesheet('/sf/sf_admin/css/main') ?>
<div id="sf_admin_container">
<h1>View MR from <?php echo $bom->getWono() ?></h1>

<div id="sf_admin_content">


<fieldset id="sf_fieldset_none" class="">

<div class="form-row">
  <?php echo label_for('mr[mrno]', __($labels['mr{mrno}']), 'class="required" ') ?>
  <div class="content<?php if ($sf_request->hasError('mr{mrno}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{mrno}')): ?>
    <?php echo form_error('mr{mrno}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>

  <!--Tien-->
  <?php $default = CommonValues::GenerateAutoNumber('MR', null, 'MR')   ?>
   <input name="mr[mrno]" id="mr_mrno" value="<?php echo $default ?>"  type="hidden">
   <input name="mr_mrno_show" id="mr_mrno_show" value="<?php echo $default ?>" size="15" disabled="disabled" type="text">
   </div>
</div>

<div class="form-row">
  <?php echo label_for('mr[bom_id]', __($labels['mr{bom_id}']), 'class="required" ') ?>
  <div class="content<?php if ($sf_request->hasError('mr{bom_id}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{bom_id}')): ?>
    <?php echo form_error('mr{bom_id}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>  
      <input name="mr_bomno_show" id="mr_bomno_show" value="<?php echo $bom->getWono() ?>" size="15" disabled="disabled" type="text">
    </div>
</div>


<div class="form-row">
  <?php echo label_for('mr[category]', __($labels['mr{category}']), '') ?>
  <div class="content<?php if ($sf_request->hasError('mr{category}')): ?> form-error<?php endif; ?>">
  <?php if ($sf_request->hasError('mr{category}')): ?>
    <?php echo form_error('mr{category}', array('class' => 'form-error-msg')) ?>
  <?php endif; ?>
     
    <select name="mr[category]" id="mr_category" disabled>
    <?php foreach (CommonValues::$gMRCategory['key'] as $key=>$category) { ?>
    <option value="<?php echo $category;  if ($category == 1) echo '" selected="1'?>"><?php echo $key ?></option>
    <?php } ?>
    </select>
    </div>
</div>



</fieldset>

<!--Tien code here
get information for MR-->
<?php
    if ($bom->getComponentgroupId()){
        $componentGroupId = $bom->getComponentgroupId();
        
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID , $componentGroupId);
        $c->add(ComponentsPeer::QTY2 , 0);
        $c->addAscendingOrderByColumn(ComponentsPeer::COMPONENT_ID);
        
        $componentList = ComponentsPeer::doSelect($c);
    }
?>



<table id ='added_components' "cellspacing="0" class="sf_admin_list">
<thead>
<tr>
      <th id="sf_admin_list_th_productcode" width="25%">Product Code
              </th>
      <th id="sf_admin_list_th_description" width="45%">Description
              </th>
      <th id="sf_admin_list_th_description" width="10%">Brand
              </th>       
      <th id="sf_admin_list_th_description" width="5%">Unit
              </th>      
      <th id="sf_admin_list_th_description">Qty to request</th>
</tr>
</thead>
<!--show component here-->

<?

if (isset($componentList) && $componentList) 
{
    $dem = 0;
    
    //neu la BOM thuong ko revise
    if (!$bom->getReviseId())
    {
        foreach ($componentList as $key=>$components) 
        {
            $comp = ComponentPeer::retrieveByPK($components->getComponentId());
            $c = new Criteria();
            $c->add(WarehousePeer::COMPONENT_ID, $comp->getId());
            $warehouse = WarehousePeer::doSelectOne($c);

            $qtytorequest = $warehouse->getQtyneeded() + $components->getQty1()
                - $warehouse->getClosing() - $warehouse->getQtyrequested()- $warehouse->getQtyordered();

            if ($qtytorequest >0)
            {
                addComponent($dem, $comp, $qtytorequest);
                $dem++;
            }
        }
    }
    else
    {
           $reviseId = $bom->getReviseId();
           $rvBom = BOMPeer::retrieveByPK($reviseId);

           if (!$rvBom) return;

           //get MR of revised BOM
           $c = new Criteria(); $c->add(MRPeer::BOM_ID , $reviseId);
           $rvMR = MRPeer::doSelectOne($c);

           $has_rvMR = $rvMR? 1: 0;
           $rvMRCompGroupId = $has_rvMR ? $rvMR->getComponentgroupId() : null;

           //get componentsbyclient of revised BOM
           $c = new Criteria(); $c->add(ComponentsByClientPeer::BOM_ID , $reviseId);
           $rvCBC = ComponentsByClientPeer::doSelectOne($c);

           $has_rvCBC = $rvCBC ? 1: 0;
           $rvCBCCompGroupId = $has_rvCBC ? $rvCBC->getComponentgroupId() : null;

           $rvBOMCompGroupId = $rvBom->getComponentgroupId();
           $BOMCompGroupId = $bom->getComponentgroupId();

           if (!$BOMCompGroupId) return;

           //get comps list of this BOM
           $c = new Criteria();    $c->add(ComponentsPeer::COMPONENTGROUP_ID , $BOMCompGroupId);
           $c->addAscendingOrderByColumn(ComponentsPeer::COMPONENT_ID);
           $BOMCompsList = ComponentsPeer::doSelect($c);

           //get comps list of revised BOM
           $c = new Criteria();    $c->add(ComponentsPeer::COMPONENTGROUP_ID , $rvBOMCompGroupId);
           $c->addAscendingOrderByColumn(ComponentsPeer::COMPONENT_ID);
           $rvBOMCompsList = ComponentsPeer::doSelect($c);

           //find comps in this BOM with revised
           foreach ($BOMCompsList as $BOMComps)  
           {
               $compId = $BOMComps->getComponentId();
               $comp = ComponentPeer::retrieveByPK($compId);

               //get warehouse
               $c = new Criteria();    $c->add(WarehousePeer::COMPONENT_ID , $compId);
               $warehouse = WarehousePeer::doSelectOne($c);

               //get MR revise comp
               $rvMRComps = null;
               if ($has_rvMR && $rvMRCompGroupId) 
               {
                   $c = new Criteria();
                   $c->add(ComponentsPeer::COMPONENT_ID , $compId);
                   $c->add(ComponentsPeer::COMPONENTGROUP_ID , $rvMRCompGroupId);
                   $rvMRComps = ComponentsPeer::doSelectOne($c);
               }

               //get CBC revise comp
               $rvCBCComps = null;
               if ($has_rvCBC && $rvCBCCompGroupId) 
               {
                   $c = new Criteria();
                   $c->add(ComponentsPeer::COMPONENT_ID , $compId);
                   $c->add(ComponentsPeer::COMPONENTGROUP_ID , $rvCBCCompGroupId);
                   $rvCBCComps = ComponentsPeer::doSelectOne($c);
               }

               //get BOM revise comp
               $c = new Criteria();    $c->add(ComponentsPeer::COMPONENT_ID , $compId);
               $c->add(ComponentsPeer::COMPONENTGROUP_ID , $rvBOMCompGroupId);
               $rvBOMComps = ComponentsPeer::doSelectOne($c);

               //if user add new component then this rvcomp will be null
               if (!$rvBOMComps)
               {
                   $bSupplied = $BOMComps->getQty2()==1? 1: 0;
                   if ($bSupplied)
                   {
                   }
                   else
                   {
                       //update qty needed
                       $warehouse->setQtyneeded($warehouse->getQtyneeded() + $BOMComps->getQty1());

                       $qtytorequest = $warehouse->getQtyneeded() - $warehouse->getClosing() - $warehouse->getQtyrequested() - $warehouse->getQtyordered();

                       if ( $qtytorequest > 0)
                       {                           
                           $dem++;
                           addComponent($dem, $comp, $qtytorequest);
                       }
                   }
               }
               //user revise this comp
               else 
               {

                   $bSupplied = $BOMComps->getQty2()==1? 1: 0;
                   $bRvSupplied = $rvBOMComps->getQty2()==1? 1: 0;

                   //but if user change supplied by client
                   if ($bSupplied && !$bRvSupplied)
                   {                       
                       //decrease qty needed
                       $warehouse->setQtyneeded($warehouse->getQtyneeded() - $rvBOMComps->getQty1()>0? $warehouse->getQtyneeded() - $rvBOMComps->getQty1(): 0);

                       //create MR comps
                       if ($has_rvMR && $rvMRComps)
                       {
                           $dem++;
                           addComponent($dem, $comp, $rvMRComps->getQty1());
                       }
                   }
                   elseif (!$bSupplied && $bRvSupplied)
                   {

                       $warehouse->setQtyneeded($warehouse->getQtyneeded() + $BOMComps->getQty1());

                       $qtytorequest = $warehouse->getQtyneeded() - $warehouse->getClosing() - $warehouse->getQtyrequested() - $warehouse->getQtyordered();

                       if( $qtytorequest > 0 ) 
                       {
                           $dem++;
                           addComponent($dem, $comp, $qtytorequest);      
                       }

                   }
                   elseif ($bSupplied && $bRvSupplied)
                   {
                   }
                   elseif (!$bSupplied && !$bRvSupplied )
                   {
                        $extraQty = $BOMComps->getQty1() - $rvBOMComps->getQty1();

                        if ($extraQty == 0) 
                        {
                            if ($has_rvMR && $rvMRComps)
                            {
                                $dem++;
                                addComponent($dem, $comp, $rvMRComps->getQty1());
                            }
                        }
                        else {

                            //update qty needed in warehouse
                            $warehouse->setQtyneeded($warehouse->getQtyneeded() + $extraQty > 0 ? $warehouse->getQtyneeded() + $extraQty : 0);

                            $qtytorequest = $warehouse->getQtyneeded() - $warehouse->getClosing() - $warehouse->getQtyrequested() - $warehouse->getQtyordered();
                            
                            if ($qtytorequest > 0 ) 
                            {
                                if ($has_rvMR && $rvMRComps) 
                                {
                                    $dem++;
                                    addComponent($dem, $comp, $rvMRComps->getQty1() + $qtytorequest);
                                }
                                else
                                {
                                    $dem++;
                                    addComponent($dem, $comp, $qtytorequest);
                                }
                            }
                            //<= 0 thi giu lai
                            else 
                            {
                                if ($has_rvMR && $rvMRComps) 
                                {
                                    $dem++;
                                    addComponent($dem, $comp, $rvMRComps->getQty1());
                                }
                            }
                        }
                   }

               }
           }


           //find comps in revised BOM with this BOM
           foreach ($rvBOMCompsList as $rvBOMComps){
               $compId = $rvBOMComps->getComponentId();
               $comp = ComponentPeer::retrieveByPK($compId);

               //get warehouse
               $c = new Criteria();    $c->add(WarehousePeer::COMPONENT_ID , $compId);
               $warehouse = WarehousePeer::doSelectOne($c);

               //get MR revise comp
               $rvMRComps = null;
               if ($has_rvMR && $rvMRCompGroupId) {
                   $c = new Criteria();
                   $c->add(ComponentsPeer::COMPONENT_ID , $compId);
                   $c->add(ComponentsPeer::COMPONENTGROUP_ID , $rvMRCompGroupId);
                   $rvMRComps = ComponentsPeer::doSelectOne($c);
               }

               //get CBC revise comp
               $rvCBCComps = null;
               if ($has_rvCBC && $rvCBCCompGroupId) {
                   $c = new Criteria();
                   $c->add(ComponentsPeer::COMPONENT_ID , $compId);
                   $c->add(ComponentsPeer::COMPONENTGROUP_ID , $rvCBCCompGroupId);
                   $rvCBCComps = ComponentsPeer::doSelectOne($c);
               }

               //get BOM  comps
               $c = new Criteria();    $c->add(ComponentsPeer::COMPONENT_ID , $compId);
               $c->add(ComponentsPeer::COMPONENTGROUP_ID , $BOMCompGroupId);
               $BOMComps = ComponentsPeer::doSelectOne($c);

               //if user add new component then this rvcomp will be null
               if (!$BOMComps)
               {
                   $bRvSupplied = $rvBOMComps->getQty2()==1? 1: 0;
                   if ($bRvSupplied)
                   {
                       //do nothing
                   }
                   else
                   {
                       //decrease qty needed
                       $warehouse->setQtyneeded($warehouse->getQtyneeded() - $rvBOMComps->getQty1() > 0 ? $warehouse->getQtyneeded() - $rvBOMComps->getQty1() : 0);
                       if ($has_rvMR && $rvMRComps)  
                       {
                            $dem++;
                            addComponent($dem, $comp, $rvMRComps->getQty1());
                       }
                   }
               }
           }           
    }
}
function addComponent($dem, $comp, $qtytorequest){

    echo "<tr class='sf_admin_row_"; echo $dem%2; echo "'>";
    echo '<input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="'; echo $comp->getId(); echo '"/>';
    echo '<td>'; echo $comp->getProductcode(); echo '</td>';
    echo '<td>'; echo $comp->getDescription(); echo '</td>';
    echo '<td>'; echo $comp->getBrand(); echo '</td>';
    echo '<td>'; echo $comp->getComponentunit(); echo '</td>';
    echo '<td><input type="text" name="qtys[]" id ="qtys" size="5" disabled value ="'; echo $qtytorequest; echo '"></td>';
    echo '</tr>';
}

?>
</table>

    <ul class="sf_admin_actions">
  <li><?php echo button_to(__('Approve'), 'approval/ApproveObject?authenticationMain=APPROVE_BOM&authenticationSub=PR_APPROVE_MR&mod=BOM&id='.$bom->getId().'&backURL=approval~1ListApprovalObject~2authenticationMain~4APPROVE_BOM~3authenticationSub~4PR_APPROVE_MR~3mod~4BOM~3backURL~4', array (
  'class' => 'sf_admin_action_approve',
  'confirm' => __('Are you sure you want to approve this BOM?'),
)) ?></li>
  <li><?php echo button_to(__('Reject'), 'approval/RejectObject?authenticationMain=APPROVE_BOM&authenticationSub=PR_APPROVE_MR&mod=BOM&id='.$bom->getId().'&backURL=approval~1ListApprovalObject~2authenticationMain~4APPROVE_BOM~3authenticationSub~4PR_APPROVE_MR~3mod~4BOM~3backURL~4', array (
  'class' => 'sf_admin_action_reject',
)) ?></li>
    </ul>

</div>

</div>
</div>
